package com.icw.resource.inner.entity;

import javax.persistence.Cacheable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.icw.basis.BasePoEntity;
import static com.icw.configuration.ConstSettings.*;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Cacheable
@Getter
@Setter
@Builder
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class IcwUser extends BasePoEntity {

	private static final long serialVersionUID = 1L;

	@Column(length = MEDIUM_LENGTH, updatable = false, nullable = false, unique = true)
	private String username;

	@Column(length = MEDIUM_LENGTH, nullable = false)
	private String password;

	@Fetch(FetchMode.JOIN)
	@ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
	@JoinTable(joinColumns = @JoinColumn, inverseJoinColumns = @JoinColumn)
	private Set<IcwRole> roles;

	public String toTokenString() {
		return this.getUsername() + "_" + this.getPassword();
	}
}
